//
//  MetionsMessageController.m
//  WeiboSpace
//
//  Created by  linan4 on 12-3-3.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "MetionsMessageController.h"
//#import "SWTableCell.h"

@implementation MetionsMessageController


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];

    UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
																				  target:self action:@selector(reload)];
	self.navigationItem.leftBarButtonItem = reloadButton;
	[reloadButton release];
    
    //weibosdk
    //创建，初始化HttpEngine
	weiboEngine = [[WeiboHttpEngine alloc] InitWeiboSpaceWithDelegate:self];
    
    timeLine = [[NSMutableArray alloc]init];
    
    
}

-(void)reload
{
    //刷新
   [self requestTimeLine:nil maxID:nil requestCount:10];
}

- (void)requestTimeLine:(NSString*)sinceId maxID:(NSString*)maxId requestCount:(int)count
{
	//这里
	//NSString *strCount = [NSString stringWithFormat:@"%i",count];
	//NSDictionary *urlParams = [[NSDictionary alloc] initWithObjectsAndKeys:sinceId, @"since_id",maxId,@"max_id",strCount,@"count", nil];
    NSMutableDictionary *urlParams = [NSMutableDictionary dictionaryWithCapacity:0];
    if (sinceId) {
        [urlParams setObject:sinceId forKey:@"since_id"];
    }
	
	if (maxId) {
        [urlParams setObject:maxId forKey:@"max_id"];
    }
    
    NSString *accessToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"Accesstoken"];
	[weiboEngine SendHttpRequest:Get requestUrl:@"2/statuses/friends_timeline.json" urlParams:urlParams bodyParams:nil accessToken:accessToken];
    
}

#pragma mark WeiboHttpDelegate
-(void) DealJsonDataFinished:(id)response
{
	if ([response isKindOfClass:[NSDictionary class]])
    {
		NSDictionary *dict = (NSDictionary *)  response;
        [timeLine addObjectsFromArray:[dict objectForKey:@"statuses"]];
		
		//重新加载
		[self.tableView reloadData];
	}
}

-(void) DealJsonDataException:(NSError*)err{

}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	if (nil != timeLine&&[timeLine count] > 0) {
		return [timeLine count]+1;
	}
	else {
		return 0;
	}
	
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *indetifier = [NSString stringWithFormat:@"%d-%d",indexPath.row,indexPath.section]; 
    NSString *CellIdentifier = timeLine != nil && indexPath.row < [timeLine count]? indetifier: @"NodateCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        NSLog(@"!new cell! %d",indexPath.row);
        NSUInteger rowIndex = [indexPath row];
        
        if (rowIndex <[ timeLine count]) {
            
            NSDictionary *statusData = [timeLine objectAtIndex:rowIndex];
            
            //cell = [[[SWTableCell alloc]initWithCell:statusData cellstyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
            
        }
        else {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
            cell.textLabel.textAlignment = UITextAlignmentCenter;
            cell.textLabel.font = [UIFont systemFontOfSize:16];
            cell.textLabel.text = @"!Load More...";            
        }
        
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    else {
        NSLog(@"old cell!");
    }
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	if(indexPath.row >= [timeLine count]) 
		return 50;
	
	UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
	CGFloat height = cell.frame.size.height;
    return height;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

